//
//  ChangeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/28.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class ChangeView: UIView {

    let oldPWView = PWInputView() //旧密码
    let newPWView = PWInputView() //新密码
    let confirmPWView = PWInputView() //确认密码
    
    let changeBtn = UIButton()
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.mineColor()
        
        oldPWView.text = oldPassword
        oldPWView.placeholder = inputOldPassword
        oldPWView.inputDelegate = self
        self.addSubview(oldPWView)
        
        newPWView.text = newPassword
        newPWView.placeholder = inputNewPassword
        newPWView.inputDelegate = self
        self.addSubview(newPWView)
        
        confirmPWView.text = confirmPassword
        confirmPWView.placeholder = inputPasswardAgain
        confirmPWView.inputDelegate = self
        self.addSubview(confirmPWView)
        
        changeBtn.backgroundColor = UIColor.lightGray
        changeBtn.setTitle(change_title, for: .normal)
        changeBtn.layer.cornerRadius = RATIO_H(maxNum: 20)
        changeBtn.layer.masksToBounds = true
        changeBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        changeBtn.isEnabled = false
        self.addSubview(changeBtn)
        
        oldPWView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
        
        newPWView.snp.makeConstraints { (make) in
            make.top.equalTo(oldPWView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.right.equalToSuperview()
            make.height.equalTo(oldPWView)
        }
        
        confirmPWView.snp.makeConstraints { (make) in
            make.top.equalTo(newPWView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.right.equalToSuperview()
            make.height.equalTo(newPWView)
        }
        
        changeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(confirmPWView.snp.bottom).offset(RATIO_H(maxNum: 30))
            make.centerX.equalToSuperview()
            make.width.equalTo(RATIO(maxNum: 300))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ChangeView : PWInputViewDelegate {
    
    //MARK: 检查输入文本
    func checkInputText() {
        
        if oldPWView.textView.text != "" && newPWView.textView.text != "" && confirmPWView.textView.text != "" {
            
            changeBtn.backgroundColor = UIColor.red
            changeBtn.isEnabled = true
        }else{
            changeBtn.backgroundColor = UIColor.lightGray
            changeBtn.isEnabled = false
        }
    }
}
